package com.yidu.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.BeanUtils;

import com.yidu.dao.RoleDao;
import com.yidu.dao.UserDao;
import com.yidu.dao.UserRoleDao;
import com.yidu.domain.Role;
import com.yidu.domain.User;
import com.yidu.domain.UserRole;
import com.yidu.domain.vo.UserVo;
import com.yidu.utils.Pages;

public class UserServiceImpl implements UserService{
	private UserDao userDao;
	private UserRoleDao userRoleDao ;
	private RoleDao roleDao ;
	

	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public UserRoleDao getUserRoleDao() {
		return userRoleDao;
	}

	public void setUserRoleDao(UserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	public RoleDao getRoleDao() {
		return roleDao;
	}

	public void setRoleDao(RoleDao roleDao) {
		this.roleDao = roleDao;
	}

	/**
	 * 增加数据
	 */
	@Override
	public void add(UserVo vo) {
		User user=new User();
		if(vo.getUserValidity().equals("on") || vo.getUserValidity().equals("true")) {
			vo.setUserValidity("有效");
		}else {
			vo.setUserValidity("无效");
		}
		Date date = new Date();
		SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
		vo.setUserTime(dateFormat.format(date));
		BeanUtils.copyProperties(vo,user);
		userDao.add(user);
		if(user.getUserId()!=null && !"".equals(user.getUserId())) {
			List<UserRole> list = userRoleDao.findUserId(user.getUserId());
			for (int i = 0; i < list.size(); i++) {
				userRoleDao.delete(list.get(i).getUserRoleId());
			}
		}
		if(vo.getRoleId()!=null && !"".equals(vo.getRoleId())) {
			String str = vo.getRoleId().split(",,")[0];
			String[] id = str.split(",");
			for (int i = 0; i < id.length; i++) {
				Role role = roleDao.findById(id[i]);
				UserRole userRole = new UserRole();
				userRole.setUser(user);
				userRole.setRole(role);
				userRoleDao.saveOrUpdate(userRole);
			}
		}
		
	}
	/**
	 * 删除数据
	 */
	@Override
	public void delete(UserVo uservo) {
		User user =  userDao.findById(uservo.getUserId());
		user.setUserValidity("无效");
		userDao.update(user);
	}
	/**
	 * 修改数据
	 */
	@Override
	public void update(UserVo vo) {
		String str = vo.getUserValidity();
		if(vo.getUserValidity().equals("on") || vo.getUserValidity().equals("true")) {
			vo.setUserValidity("有效");
		}else {
			vo.setUserValidity("无效");
		}
		User users =  userDao.findById(vo.getUserId());
		if(str.equals("true") || str.equals("false")) {
			users.setUserValidity(vo.getUserValidity());
			userDao.update(users);
		}else {
			vo.setUserTime(users.getUserTime());
			if(vo.getUserImg()==null || vo.getUserImg().equals("")) {
				vo.setUserImg(users.getUserImg());
			}
			BeanUtils.copyProperties(vo,users);
			userDao.update(users);
			if(users.getUserId()!=null && !"".equals(users.getUserId())) {
				List<UserRole> list = userRoleDao.findUserId(users.getUserId());
				for (int i = 0; i < list.size(); i++) {
					userRoleDao.delete(list.get(i).getUserRoleId());
				}
			}
			if(vo.getRoleId()!=null && !"".equals(vo.getRoleId())) {
				String strs = vo.getRoleId().split(",,")[0];
				String[] id = strs.split(",");
				for (int i = 0; i < id.length; i++) {
					Role role = roleDao.findById(id[i]);
					UserRole userRole = new UserRole();
					userRole.setUser(users);
					userRole.setRole(role);
					userRoleDao.saveOrUpdate(userRole);
				}
			}
		}
		
	}
	/**
	 * 查询全部数据
	 */
	@Override
	public List<UserVo> findAll(UserVo emp, Pages pages) {
		User params = new User();
		BeanUtils.copyProperties(emp, params);
		List<User> list = userDao.findAll(params,pages);
		List<UserVo> listVo = new ArrayList<UserVo>();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			User po = (User) iterator.next();
			UserVo vo = new UserVo();
			BeanUtils.copyProperties(po, vo);
			listVo.add(vo);
		}
		return listVo;
	}
	/**
	 * 根据id查询数据
	 */
	@Override
	public UserVo findById(String id) {
		User po=userDao.findById(id);
		UserVo vo=new UserVo();
		BeanUtils.copyProperties(po,vo);
		List<UserRole> list = userRoleDao.findUserId(vo.getUserId());
		String ids = "";
		for (int i = 0; i < list.size(); i++) {
			ids += list.get(i).getRole().getRoleId()+",";
		}
		vo.setRoleId(ids);
		return vo;
	}
	/**
	 * 查询行数
	 */
	@Override
	public Long count(UserVo userVo) {
		User user = new User();
		BeanUtils.copyProperties(userVo, user);
		return userDao.count(user);
	}
	/**
	 * 登录查询用户和密码
	 */
	@Override
	public UserVo findAllUser(UserVo userVo) {
		User user = new User();
		BeanUtils.copyProperties(userVo, user);
		List<User> list = userDao.findAllUser(user);
		UserVo userVos = new UserVo();
		if(list.size()>0) {
			User users = list.get(0);
			if(users.getUserValidity().equals("有效")) {
				BeanUtils.copyProperties(users, userVos);
			}
		}
		return userVos;
	}
	
}
